package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 经销商消息设置
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:41
 */
public class DistributorContactPushSet extends AbstractPushSet {
    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

    private static Integer oneDay = 86400;

    private String pushObject;

    private String pushTimeObject;

    public String getPushObject() {
        return pushObject;
    }

    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    public String getPushTimeObject() {
        return pushTimeObject;
    }

    public void setPushTimeObject(String pushTimeObject) {
        this.pushTimeObject = pushTimeObject;
    }

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime, String attrEntity) {
        setPushObject(pushObject);
        setPushTimeObject(pushTime);
        return new PushSetEntity(){{
            setAddTime(addTime);
            setAttrEntity(attrEntity);
            setCorpid(corpid);
            setDel(del);
            setEnable(enable);
            setId(id);
            setPushObject(pushObject);
            setPushTime(pushTime);
            setRefEntity(refEntity);
            setSubType(subType);
            setType(type);
            setUpdateTime(updateTime);
        }};
    }

    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType, PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.DISTRIBUTOR_CONTACT_PUSH.getCode();
        Map<String, Object> param = new HashMap<String, Object>(16){{
            put("corpid", corpid);
            put("type", type);
            put("subType", subType);
        }};

        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if (pushSetList.isEmpty()) {
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);

            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.DISTRIBUTOR_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setEnable(0);
                pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_ADD_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_CONTACT_NEW));

                //type
                pushObject.put("type", new JSONArray(){{
                    add(new JSONObject(){{
                        put("name", PushObjectTypeEnum.MAINUSER.getName());
                        put("enable", 1);
                        put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                    }});
                    add(new JSONObject(){{
                        put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                        put("enable", 1);
                        put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                    }});
                }});

                //manager
                pushObject.put("manager", new JSONObject(){{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //dep
                pushObject.put("dep", new JSONObject(){{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //role
                pushObject.put("role", new JSONObject(){{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //user
                pushObject.put("user", new JSONObject(){{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});
            }
            pushSetEntity.setPushObject(pushObject.toJSONString());
            pushSetList.add(pushSetEntity);
        } else {
            pushSetEntity = pushSetList.get(0);

            if (Objects.equals(PushTypeEnum.DISTRIBUTOR_CONTACT_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_CONTACT_ADD_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_CONTACT_NEW));

            }

        }
        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, String attr, PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return null;
    }
}
